package com.qf.thread.callable;

import java.util.concurrent.*;

public class CallableDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        // 启动一个先从帮助我们去计算0~30和

        // 创建一个CallableThread
        CallableThread thread = new CallableThread();

        // 2.创建一个Futuretask
        FutureTask<String> futureTask = new FutureTask<String>(thread);

        //3.启动一个先从运行futuretask
        new Thread(futureTask).start();

        ExecutorService executorService = Executors.newFixedThreadPool(10);

        // 4.得到线程运行的返回结果
        String s = futureTask.get(); // 这个方法是个阻塞方法
        System.out.println(s);
    }
}

class CallableThread implements Callable<String> {
    @Override
    public String call() throws Exception {
        int sum = 0;
        for (int i = 0; i < 30; i++) {
            sum += i;
            Thread.sleep(500);
        }
        return "sum:" + sum;
    }
}
